﻿using QuanLyThuVien.DTO;
using QuanLyThuVien.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuanLyThuVien.BLL
{
    //----------------------------------------------------------------------
    //Desc: class xử lý cho bảng DAUSACH
    //----------------------------------------------------------------------
    public class BLLDauSach
    {
        //----------------------------------------------------------------------
        //Desc: constructor mặc định
        //----------------------------------------------------------------------
        public BLLDauSach()
        { }

        //----------------------------------------------------------------------
        //Desc: lấy danh sách đầu sách
        //----------------------------------------------------------------------
        public List<DTODauSach> LayDSDauSach()
        {
            DALDauSach _dalDauSach = new DALDauSach();
            return _dalDauSach.LayDSDauSach();
        }

        //----------------------------------------------------------------------
        //Desc: lấy đầu sách
        //----------------------------------------------------------------------
        public DTODauSach LayDauSach(string madausach)
        {
            DALDauSach _dalDauSach = new DALDauSach();
            DTODauSach dausach = _dalDauSach.LayDauSach(madausach);
            if (dausach == null)
                return null;

            DALTheLoai _dalTheLoai = new DALTheLoai();
            string tentl = _dalTheLoai.LayTenTheLoai(dausach.TheLoai.MaTheLoai);
            if (tentl == null)
                return null;
            dausach.TheLoai.TenTheLoai = tentl;

            DALCT_TacGia _dalCT_TacGia = new DALCT_TacGia();
            dausach.DSCT_TacGia = _dalCT_TacGia.LayDSCT_TacGia(madausach);
            if (dausach.DSCT_TacGia == null)
                return null;

            DALTacGia _dalTacGia = new DALTacGia();
            for (int i = 0; i < dausach.DSCT_TacGia.Count; i++)
                dausach.DSCT_TacGia[i].TacGia.TenTG = _dalTacGia.LayTenTG(dausach.DSCT_TacGia[i].TacGia.MaTG);

            return dausach;
        }

        //----------------------------------------------------------------------
        //Desc: phát sinh mã đầu sách
        //----------------------------------------------------------------------
        public bool PhatSinhMaDauSach(ref DTODauSach dausach)
        {
            DALDauSach _dalDauSach = new DALDauSach();
            int stt = _dalDauSach.DemDauSach();
            if (stt < 0)
                return false;

            if (stt == 0)
                stt++;
            int dem = _dalDauSach.DemMaDauSach(stt.ToString("D3"));
            while (dem > 0)
            {
                stt++;
                dem = _dalDauSach.DemMaDauSach(stt.ToString("D3"));
            }
            if (dem < 0)
                return false;

            dausach.MaDauSach = stt.ToString("D3");
            for (int i = 0; i < dausach.DSCT_TacGia.Count; i++)
                dausach.DSCT_TacGia[i].MaDauSach = dausach.MaDauSach;
            return true;
        }

        //----------------------------------------------------------------------
        //Desc: lấy tên thể loại
        //----------------------------------------------------------------------
        public string LayTenTL(string madausach)
        {
            DALDauSach _dalDauSach = new DALDauSach();
            return _dalDauSach.LayTenTL(madausach);
        }

        //----------------------------------------------------------------------
        //Desc: lấy danh sách tên tác giả
        //----------------------------------------------------------------------
        public List<string> LayDSTenTG(string madausach)
        {
            DALDauSach _dalDauSach = new DALDauSach();
            return _dalDauSach.LayDSTenTG(madausach);
        }

        //----------------------------------------------------------------------
        //Desc: cập nhật đầu sách
        //----------------------------------------------------------------------
        public bool CapNhat(DTODauSach dausach)
        {
            DALDauSach _dalDauSach = new DALDauSach();
            if (_dalDauSach.CapNhat(dausach))
                return true;
            else
                return false;
        }

        //----------------------------------------------------------------------
        //Desc: kiểm tra tồn tại sách trong đầu sách
        //----------------------------------------------------------------------
        public bool KiemTraTonTaiSach(string madausach)
        {
            DALDauSach _dalDauSach = new DALDauSach();
            int dem = _dalDauSach.DemSoSach(madausach);
            if (dem != 0)
                return true;
            else
                return false;
        }

        //----------------------------------------------------------------------
        //Desc: xóa đầu sách
        //----------------------------------------------------------------------
        public bool Xoa(DTODauSach dausach)
        {
            DALDauSach _dalDauSach = new DALDauSach();
            return _dalDauSach.Xoa(dausach);
        }
    }
}
